package com.mach.platform.api.controller;

import com.mach.platform.service.CacheServiceImpl;
import com.mach.platform.api.service.LoginServiceImpl;
import com.mach.platform.api.util.AppletsUtil;
import com.mach.platform.api.util.ImageUtil;
import com.mach.platform.domain.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Slf4j
@Api(tags = {"小程序登录页面接口"})
@RestController
@RequestMapping("/api")
public class WxLoginCtrl {

    @Autowired
    private LoginServiceImpl loginService;

    @Autowired
    private CacheServiceImpl cacheService;

    /**
     * 手机短信验证码有效时间
     */
    private final long timeOut = 5 * 60 * 1000;

    /**
     * 获取微信小程序用户的openId和sessionKey
     * @param code 临时登录凭证
     * @return
     */
    @ApiOperation(value = "获取微信小程序用户的openId和sessionKey")
    @PostMapping(value = "/getUserOpenIdInfo")
    public Response getUserOpenIdInfo(String code) {
        Response result = AppletsUtil.getAppletsUserInfo(code);
        if (result.getStatus() == 200) {
            // 把openId信息写进缓存
            Map map = (Map) result.getData();
            String openId = (String) map.get("openId");
            cacheService.putCache(openId, openId);
        }
        return result;
    }

    /**
     * 发送短信验证码
     * @param phone 手机号
     * @return
     */
//    @ApiOperation(value = "发送短信验证码")
//    @PostMapping("/sendSms")
//    public Response sendSms(HttpServletRequest request, String phone) {
//        Map result = AppletsUtil.sendSms(phone);
//        if (!result.get("code").toString().equals("0")) {
//            return Response.error("发送短信验证码失败！");
//        }
//        HttpSession session = request.getSession();
//        JSONObject json = new JSONObject();
//        json.put("verifyCode", result.get("verifyCode"));
//        json.put("createTime", System.currentTimeMillis());
//        session.setAttribute(phone, json);
//        return Response.ok("手机验证码已发送！");
//    }


    /**
     * 微信小程序登录，上传头像
     * @param nickName 用户昵称
     * @param avatarUrl 头像网络地址
     * @return
     */
    @ApiOperation(value = "微信小程序手机号登录验证")
    @PostMapping("/login")
    public Response login(HttpServletRequest request, String nickName, String avatarUrl) {
        String openId = request.getHeader("openId");
//        JSONObject object = (JSONObject) session.getAttribute(user.getPhone());
//        if (!code.equals(object.getString("verifyCode"))) {
//            return Response.ok("手机验证码错误！");
//        }
//        long curTime = System.currentTimeMillis();
//        if (curTime - object.getLong("createTime") > timeOut) {
//            session.removeAttribute(user.getPhone()); // 清除验证码信息
//            return Response.error("短信验证码已过期，请重新获取！");
//        }
        if (!ImageUtil.uploadUrlAvatar(openId, avatarUrl)) {
            return Response.error("上传头像失败");
        }
        loginService.saveUser(openId, nickName);
//        session.removeAttribute(user.getPhone()); // 清除验证码信息
        return Response.ok("登录成功");
    }

}
